# meet_at.models

import pytz
from google.appengine.ext import db
from kay.auth.models import GoogleUser
from kay.conf import settings

# Create your models here.

ANONYMOUS = u'Anonymous'

class MeetAtUser(GoogleUser):
  display_name = db.StringProperty(required=True,default=ANONYMOUS)
  tz = db.StringProperty(choices=pytz.all_timezones, required=True,
                         default=settings.DEFAULT_TIMEZONE,
                         indexed=False)

class KeyNameAsKeyMixin(object):
  @classmethod
  def get_key_name(cls, name):
    return "%s:%s" % (cls.__name__.lower()[0],name)

  @classmethod
  def get_by_name(cls, name):
    return cls.get_by_key_name(cls.get_key_name(name))


class Tag(KeyNameAsKeyMixin, db.Model):
  name = db.StringProperty(required=True)
  count = db.IntegerProperty(required=True, default=1)
  created = db.DateTimeProperty(auto_now_add=True)
  updated = db.DateTimeProperty(auto_now=True)


class Event(KeyNameAsKeyMixin, db.Model):
  name = db.StringProperty(required=True)
  slug = db.StringProperty(required=True)
  owner = db.ReferenceProperty(MeetAtUser, required=True)
  staffs = db.ListProperty(db.Key)
  created = db.DateTimeProperty(auto_now_add=True)
  updated = db.DateTimeProperty(auto_now=True)

  open_datetime = db.DateTimeProperty()
  start_datetime = db.DateTimeProperty()
  end_datetime = db.DateTimeProperty()

  capacity = db.IntegerProperty()
  available = db.IntegerProperty()
  fee = db.IntegerProperty(default=0)

  description = db.TextProperty()
  site = db.LinkProperty(indexed=False)
  place = db.StringProperty(indexed=False)

  has_party = db.BooleanProperty(default=True)
  party_price = db.IntegerProperty(indexed=False)
  party_place = db.StringProperty(indexed=False)

  tags = db.StringListProperty()

  def can_be_managed_by(self, user):
    if self.owner.key() == user.key():
      return True
    for staff in self.staffs:
      if staff == user.key():
        return True
    return False


class Participant(db.Model):
  """
  Parent: Event
  key_name: MeetAtUser.key().name()
  """
  attend_party = db.BooleanProperty(default=False)
  note = db.TextProperty()

  waiting = db.BooleanProperty(default=False)
  created = db.DateTimeProperty(auto_now_add=True)
  updated = db.DateTimeProperty(auto_now=True)
    

class Application(db.Model):
  """
  """
  event = db.ReferenceProperty(Event)
  user = db.ReferenceProperty(MeetAtUser)
  attend_party = db.BooleanProperty(default=False)
  note = db.TextProperty()

  created = db.DateTimeProperty(auto_now_add=True)
  updated = db.DateTimeProperty(auto_now=True)
